package com.medical.sys.service.impl;


import com.medical.sys.mapper.RoleMapper;
import com.medical.sys.mapper.UserMapper;

import com.medical.sys.model.Role;
import com.medical.sys.model.User;

import com.medical.sys.service.UserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserMapper userMapper;

	public List<User> selectUsersByRoleName(String roleName) {
		List<User> users = new ArrayList<User>();
		List<Role> roles = roleMapper.selectByName(roleName);
		for(Role r : roles){
			users.addAll(selectUsersByRoleId(r.getId()));
		}
		return users;
	}

	public List<User> selectUsersByRoleId(String roleId) {
		if(!StringUtils.isEmpty(roleId)){
			roleId = "%"+roleId+"%";
		}
		return userMapper.selectByRoleId(roleId);
	}
	public List<String> selectDoctorName(String roleName){
		List<String> doctornames = new ArrayList<String>();
		List<User> users = new ArrayList<User>();
		users.addAll(selectUsersByRoleName(roleName));
		for (User u: users){
			doctornames.add(u.getFullname());
		}
		return doctornames;
	}

	public User selectByPrimaryKey(String id) {
		return userMapper.selectByPrimaryKey(id);
	}
}
